package com.hxk.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * springboot异常处理方式二
 * 使用@ExceptionHandler处理指定异常跳转到指定页面
 * 缺点：只能控制本controller，不能控制其他controller
 * @author Cactus
 */
@Controller
public class DemoController1 {

	@RequestMapping("/demo")
	public String demo() {
		String str = null;
		str.length();
		return "index";
	}
	
	@RequestMapping("/demo1")
	public String demo1() {
		int a = 10/0;
		return "index";
	}
	/**
	 * 处理该controller中出现的空指针异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(java.lang.NullPointerException.class)
	public ModelAndView nullException(Exception e) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("error", e.toString());
		mav.setViewName("error1");
		return mav;
	}
	/**
	 * 处理该controller中出现的算数异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(java.lang.ArithmeticException.class)
	public ModelAndView arithmeticException(Exception e) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("error", e.toString());
		mav.setViewName("error2");
		return mav;
	}
}
